<template>
    <div class="goods-container">
        <h1>商品展示列表</h1>
        <div class="goods-list">
            <div v-for="item in goods" :key="item.id" class="goods-item">
                <router-link :to="{ name: 'productDetail', params: { id: item.id } }" class="goods-link">
                    <h3>{{ item.id }}、{{ item.name }}</h3>
                </router-link>
                <p class="price">价格：{{ item.price }}</p>
                <p class="sales">销量：{{ item.sales }}</p>
            </div>
        </div>
    </div>
</template>

<script setup>
import axios from 'axios';
import { onMounted, ref } from 'vue';

let goods = ref([]);
onMounted(() => {
    axios.get('http://127.0.0.1:8000/app/goods/').then(res => {
        goods.value = res.data;
    })
})

</script>

<style>
.goods-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h1 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 2em;
}

.goods-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
}

.goods-item {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 15px;
    width: calc(33.333% - 20px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease-in-out;
    text-align: center;
}

.goods-item:hover {
    transform: translateY(-10px);
}

.goods-link {
    text-decoration: none;
    color: #333;
    display: block;
    margin-bottom: 10px;
}

.goods-image {
    width: 100%;
    height: 200px;
    overflow: hidden;
    border-radius: 8px;
    margin-bottom: 10px;
}

.goods-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.goods-info {
    text-align: left;
}

.goods-info h3 {
    font-size: 1.2em;
    margin: 0;
    color: #333;
}

.goods-info .price, .goods-info .sales {
    margin: 5px 0;
    color: #555;
}

.goods-info .price {
    font-weight: bold;
    color: #ff5722;
}

.goods-info .sales {
    font-style: italic;
}
</style>